﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AwpAdminTool;
using System.Net.Battlefield3;


namespace AdminPlugin
{
    public class PluginMain : Plugin
    {
        public PluginMain(AwpAdmin awpAdmin) : base(awpAdmin)
        {
            //initialization time :D

            //register commands
            ParentAdmin.RegisterCommand("kick", "/kick <name>; Kicks a player from the server.");
            ParentAdmin.RegisterCommand("ban", "/ban <name>; Bans a player for a round.");
            ParentAdmin.RegisterCommand("setadmin", "/setadmin <name> <level>; Sets the admin rank of the player.");
            ParentAdmin.RegisterCommand("move", "/move <name> [team ID]; Move a player to the opposite team or to a specific team.");
        }

        protected override void OnCommand(CommandEventArgs e)
        {
            switch (e.Command)
            {
                case "kick":
                    if (e.Parameters.Count > 0 && //player supplied
                        ParentAdmin.Client.Players.PlayerExists(e.Parameters[0])) //player exists
                    {
                        ParentAdmin.Client.Players[e.Parameters[0]].Kick();
                    }
                    break;

                case "move":
                    if (e.Parameters.Count > 0 &&
                        ParentAdmin.Client.Players.PlayerExists(e.Parameters[0])) //exists
                    {
                        Player p = ParentAdmin.Client.Players[e.Parameters[0]];
                        if (e.Parameters.Count > 1) //team supplied?
                        {
                            int team = 0;
                            if (Int32.TryParse(e.Parameters[1], out team)) //is number
                                p.ChangeTeam(team);

                        }
                        else //not supplied, assume there are 2 teams
                            p.ChangeTeam(p.TeamId == 1 ? 2 : 1);

                    }
                    break;

                case "ban":
                    if (e.Parameters.Count > 0 && //supplied
                        ParentAdmin.Client.Players.PlayerExists(e.Parameters[0])) //exists
                    {
                        ParentAdmin.Client.Players[e.Parameters[0]].RoundBan(1);
                    }
                    break;

                case "setadmin":
                    int rank = 0;
                    if (e.Parameters.Count > 1 && //player and rank supplied
                        ParentAdmin.Client.Players.PlayerExists(e.Parameters[0]) && //exists
                        Int32.TryParse(e.Parameters[1], out rank) //rank is number
                        )
                        ParentAdmin.Client.Players[e.Parameters[0]].GetAdmin().Rank = rank;
                    break;
            }
        }
    }
}
